class Solution {
    public int subarraySum(int[] nums, int k) {
        if (nums.length == 0) {
            return 0;
        }
        int left = 0;
        int right = 1;
        int sum = nums[0];
        int count = 0;
        if (nums[0] == k && nums.length == 1) {
            return 1;
        }
        while (right < nums.length && left <= right) {
            if (sum == k) {
                sum = sum - left;
                left++;
                count++;
            } else if (sum > k) {
                sum = sum - left;
                left++;
            } else {
                sum = sum + nums[right];
                right++;

            }
        }
        return count;
    }
}
